package com.aiti.lulian.factory;


import com.aiti.lulian.enums.RankIncreaseTaskStrategyEnum;
import com.aiti.lulian.strategy.task.RankIncreaseTaskStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class RankIncreaseTaskFactory {

    @Autowired
    private Map<String, RankIncreaseTaskStrategy> strategyMap = new HashMap<>();

    private final Map<String, RankIncreaseTaskStrategy> cachedStrategyMap = new HashMap<>();

    @PostConstruct
    public void init() {

        for (RankIncreaseTaskStrategy strategy : strategyMap.values()) {
            RankIncreaseTaskStrategyEnum strategyEnum = strategy.getStrategy();
            cachedStrategyMap.put(strategyEnum.getType(), strategy);
        }
    }


    public RankIncreaseTaskStrategy getStrategy(String type) {
        if (type == null) return null;
        return cachedStrategyMap.get(type);
    }


    public boolean supports(String type) {
        return getStrategy(type) != null;
    }
}